<template>
  <div>
    <basic-container>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>内容审核</el-breadcrumb-item>
        <el-breadcrumb-item>用户头像</el-breadcrumb-item>
      </el-breadcrumb>
    </basic-container>
    <basic-container> 
      <avue-crud ref="crud" :data="data" v-model="form" :option="option" :page.sync="page" @on-load="getList" @refresh-change="refreshChange" @search-reset="from = {}" @search-change="searchChange" @selection-change="selectionChange">
        <!-- 自定义按钮 -->
        <template slot-scope="scope" slot="menuLeft">
          <el-button type="primary" icon="el-icon-edit" size="small" plain @click.stop="editSelect(true)">通过</el-button>
          <el-button type="danger" icon="el-icon-delete" size="small" plain @click.stop="editSelect(false)">拒绝</el-button>
          <br />
          <!-- <el-tag> 已通过n条 未通过n条 待审核n条</el-tag> -->
        </template>
        <!-- 自定义操作栏 -->
        <template slot-scope="scope" slot="menu">
          <el-button icon="el-icon-edit" type="text" size="small" @click.stop="adopt(scope)">通过</el-button>
          <el-button icon="el-icon-delete" type="text" size="small" @click.stop="refuse(scope)">拒绝</el-button>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { common } from "@/api/cat/httpRequest";
import { catUrl } from "@/config/env";
import { appUserUpdate as baseURL } from "@/api/cat/baseUrls"; 
 
export default {
  data () {
    return {
      page: {
        total: 0,
        pageSize: 10,
      },
      type: true,
      data: [],
      params: {},
      form: {

      },
      // 默认查询条件
      defalut: {
        'check_type': '1',
        'orderByList': 'create_time&DESC'
      },
      // 勾选选中的内容
      checks: [],
      option: {
        delBtn: false,
        editBtn: false,
        searchBtn: true,
        addBtn: false,
        selection: true,
        border: true,
        column: [
          {
            label: "id",
            prop: "id",
            type: "input",
            hide: true
          },
          {
            label: "日期",
            prop: "create_time",
            type: "daterange",
            search: true,
            required: true,
          },
          {
            label: "版本号",
            prop: "app_version",
            type: "input",
            search: true,
          },
          {
            label: "渠道号",
            prop: "user_channel",
            type: "input",
          },
          {
            label: "用户id",
            prop: "user_id",
            type: "input",
            search: false,
          },
          {
            label: "用户头像",
            prop: "check_content",
            type: "upload",
            listType: 'picture-img',
            search: false,
          },
          {
            label: "审核状态",
            prop: "status",
            type: "select",
            search: false,
            dicData: [
              {
                label: "<font style='color:red'>待审核</font>",
                value: 0,
              },
              {
                label: "已通过",
                value: 1,
              },
              {
                label: "未通过",
                value: 2,
              },
            ],
          },
          {
            label: "审核状态",
            prop: "statu",
            type: "select",
            search: true,
            hide:true,
            dicData: [
              {
                label: "待审核",
                value: 0,
              },
              {
                label: "已通过",
                value: 1,
              },
              {
                label: "未通过",
                value: 2,
              },
            ],
          },
          {
            label: "最后操作信息",
            prop: "edit_time",
            type: "input",
            addDisplay: false,
            editDisplay: false,
            addDisabled: false,
            editDisabled: true,
            readonly: true,
          },
        ],
      },
    };
  },
  methods: {
    /**获取列表 */
    async getList () {
      this.loading = true;
      //清空表格列表
      this.data = [];
      //发送请求获取数据
      let response = await common(
        `${baseURL.baseUrl}/pageAndParam`,
        { ...this.params, ...this.defalut },
        this.page,
        "POST"
      );
      
      this.page = response.page;
      this.data = response.data;
      this.loading = false;
    },
    /**搜索 */
    searchChange (params, done) {
      params.create_time = `${params.create_time ? "!<>!" + params.create_time : ""}`
      params.app_version = `%${params.app_version ? params.app_version : ""}%`
      params.status = params.statu;
      delete params.statu;
      this.params = params;
      this.page.currentPage = 1;
      this.getList();
      if (done) done();
    },
    /**勾选所有 */
    selectionChange (row) {
      this.checks = row
    },
    //某个不通过
    async refuse (scope) {
      if(scope.row.status==2){
        return this.$message.error("已是该状态,不可重复修改");
      }
      if(scope.row.status==1){
        return this.$message.error("不可拒绝已通过的申请");
      }
      this.$confirm(`<strong>拒绝</strong> 该用户头像修改申请, 是否继续?`, '王牌特战小提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        dangerouslyUseHTMLString: true,
        type: 'warning'
      }).then(async () => {
        let userInfo = this.$store.state.user.userInfo
        scope.row.edit_admin_id = userInfo.id ? userInfo.id : null
        scope.row.edit_admin_name = userInfo.username
        scope.row.status = 2
        scope.row.edit_time = new Date()
        let data = await common(baseURL.baseUrl + "/update", scope.row, null, "post");
        this.getList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        });          
      });
    },
    //某个通过
    async adopt (scope) {
      if(scope.row.status==1){
        return this.$message.error("已是该状态,不可重复修改");
      }
      if(scope.row.status==2){
        return this.$message.error("不可通过已拒绝的申请");
      }
      this.$confirm(`<strong>通过</strong> 该用户头像修改申请, 是否继续?`, '王牌特战小提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        dangerouslyUseHTMLString: true,
        type: 'warning'
      }).then(async () => {
        let userInfo = this.$store.state.user.userInfo
        scope.row.edit_admin_id = userInfo.id ? userInfo.id : null
        scope.row.edit_admin_name = userInfo.name
        scope.row.status = 1
        scope.row.edit_time = new Date()
        let data = await common(baseURL.baseUrl + "/update", scope.row, null, "post");
        this.getList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        });          
      });
    },
    /**
     * 全选的通过或拒绝
     * type : 1表示通过,2表示拒绝
     */
    async editSelect (type, falg) {
      let arr = this.checks.filter(item=> item.status==(type ? 1 : 2));
      if(arr.length>0){
        return this.$message.error("选择内容存在该状态,不可修改");
      }
      let arrtwo = this.checks.filter(item=> item.status==2);
      if(arrtwo.length>0){
        return this.$message.error("不可通过已拒绝的申请");
      }

      this.$confirm(`<strong>${type?"通过":"拒绝"}</strong> 选中的<strong>(${this.checks.length}条)</strong>用户头像修改申请, 是否继续?`, '王牌特战小提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        dangerouslyUseHTMLString: true,
        type: 'warning'
      }).then(async () => {
        this.checks.forEach(item => item.status = type ? 1 : 2)
        let data = await common(baseURL.baseUrl + "/updateAll", this.checks, null, "post");
        this.getList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        });          
      });
    },
  },
};
</script>